﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SliderValueChange : MonoBehaviour {

    public Slider m_slider;

    long m_totalValue = 0;//实时更新

    long m_animValue = 0;//延迟更新

    public long curDegreeValue //当前等级内的值，实时更
    { 
        get
        {
            if (maxValueList!=null)
            {
                var lastDegreeIndex = 0;
                if (curIndex > 0)
                {
                    lastDegreeIndex = curIndex - 1;
                    return m_totalValue - maxValueList[lastDegreeIndex];
                }
            }
            return m_totalValue;
           
        } 
    }
    public List<long> maxValueList = new List<long>();

  //  public int maxValue;
    public  long CurDegreeMaxValue//当前等级的最大值
    { 
        get
        {
            if (maxValueList.Count == 0)
            {
                return 0;
            }
            if (maxValueList.Count == 1)
            {
                return maxValueList[0];//不可升级的情况
            }
            var lastDegreeIndex = 0;

            if (curIndex > 0)
            {
                lastDegreeIndex = curIndex - 1;

                return maxValueList[curIndex] - maxValueList[lastDegreeIndex];
            }

            return maxValueList[curIndex];
        } 
    }

    long curDegreeAnimValue //当前等级内的值,在动画中更新
    {
        get
        {
            if (maxValueList != null)
            {
                var lastDegreeIndex = 0;
                if (curIndex > 0)
                {
                    lastDegreeIndex = curIndex - 1;
                    return m_animValue - maxValueList[lastDegreeIndex];
                }
            }
            return m_animValue;

        }
    }



    public UnityEngine.GameObject upgradeFXPref;

    /// <summary>
    /// 最大值是一个列表，可以升级,此列表各级的值应是改级总量
    /// </summary>
    /// <param name="m_value"></param>
    /// <param name="maxList"></param>
    public void Init(long m_value, List<long> maxList) 
    {
        maxList.Sort((a,b)=>a.CompareTo(b));
        this.maxValueList = maxList;

        m_slider = GetComponent<Slider>();
        
        m_totalValue = m_value;

        preMaxIndex = curIndex;

        SetSliderValue();

    }

    /// <summary>
    /// 最大值只有一个，不会升级
    /// </summary>
    /// <param name="m_value"></param>
    /// <param name="max"></param>
    public void Init(long m_value, long max)
    {
        this.maxValueList = new List<long> {0, max };//第一位是0，第二位是最大值

        m_slider = GetComponent<Slider>();

        m_totalValue = m_value;

        SetSliderValue();
    }

    public void OnUpdateValue(long targetValue)
    {
        if (!gameObject.activeInHierarchy) { return; }
        if (targetValue <= m_totalValue)
        {
            return;
        }
        if (maxValueList.Count>=1)
            if (targetValue > GetTotalMaxValue())
                targetValue = GetTotalMaxValue();

       StartCoroutine(PlayChangeValueToTarget(targetValue));
    }

    float speed = 1;
    int preMaxIndex = 0;
    /// <summary>
    /// 播放滑条动画
    /// </summary>
    /// <param name="targetValue"></param>
    /// <returns></returns>
    IEnumerator PlayChangeValueToTarget(long targetValue) 
    {
        preMaxIndex = curIndex;
        m_animValue = m_totalValue;
        m_totalValue = targetValue;
        while (m_animValue < targetValue)
        {
            var curDegreeMaxValue = CurDegreeMaxValue;
            var step = Mathf.Min(speed * ((float)curDegreeMaxValue / 30f), targetValue - m_animValue);
            step = Mathf.Max(step,1);
            m_animValue += (long)step;

            SetSliderValue(true);

            if (preMaxIndex != curIndex)
            {
                m_slider.value = 0;
                preMaxIndex = curIndex;

                if (maxValueList.Count > 2)
                {
                    StartCoroutine(ShowUpgradeFX());
                }
            }
            yield return null;
        }
        m_animValue = targetValue;
        yield return 0;
        SetSliderValue();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="anim">是否是动画中</param>
    void SetSliderValue(bool anim = false) 
    {
        if (m_slider == null)
            m_slider = GetComponent<Slider>();
        if (m_totalValue >= GetTotalMaxValue())//到了最大值总显示为满
        {
            m_slider.transform.Find("Fill Area").gameObject.SetActive(true);
            m_slider.value = 1;
            return;
        }
        if (anim)
        {
            m_slider.value = (float)curDegreeAnimValue / (float)CurDegreeMaxValue;
        }
        else
        {
            m_slider.value = (float)curDegreeValue / (float)CurDegreeMaxValue;

            if (m_slider.value <= 0)
            {
                m_slider.transform.Find("Fill Area").gameObject.SetActive(false);
            }
            else
            {
                m_slider.transform.Find("Fill Area").gameObject.SetActive(true);
            }
        }
        
    }


    public int curIndex
    {
        get
        {
            if (maxValueList.Count == 1)
            {
                return 0;//不可升级的情况
            }
            for (int i = 0; i < maxValueList.Count; i++)
            {
                if (m_totalValue < maxValueList[i])
                {
                    return i;
                }
            }
            return 0;
        }
    }

    public long GetTotalMaxValue() 
    { 
        if(maxValueList.Count > 0)
        {
            return maxValueList[maxValueList.Count - 1];
        }
        return 0;
    }

    IEnumerator ShowUpgradeFX() 
    {
        if (upgradeFXPref != null)
        {
            UnityEngine.GameObject fx = UnityEngine.GameObject.Instantiate(upgradeFXPref) as UnityEngine.GameObject;
            fx.transform.SetParent(transform);
            fx.transform.localScale = Vector3.one;
            fx.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
            yield return new WaitForSeconds(0.5f);
            Destroy(fx.gameObject);
        }
        yield return 0;
    }

}
